package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.DataDictionary;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IDataDictionaryService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import cn.wolfcode.crm.util.PermissionName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("dataDictionary")
public class DataDictionaryController {
    @Autowired
    private IDataDictionaryService dataDictionaryService;

    @RequiresPermissions("dataDictionary:index")
    @PermissionName("字典首页")
    @RequestMapping("")
    public String index(){
        return "dataDictionary/list";
    }

    @RequiresPermissions("dataDictionary:list")
    @PermissionName("字典数据")
    @RequestMapping("list")
    @ResponseBody
    public PageResult list(QueryObject qo){
        List<DataDictionary> list = dataDictionaryService.list();
        PageResult p = new PageResult();
        p.setTotal(list.size());
        p.setRows(list);
        return p;

    }

    @RequiresPermissions("dataDictionary:save")
    @PermissionName("菜单保存")
    @RequestMapping("save")
    @ResponseBody
    public JsonResult save(DataDictionary entity){
        JsonResult result = new JsonResult();
        try {
            dataDictionaryService.save(entity);
        }catch (Exception e){
            e.printStackTrace();
            result.markMsg("保存操作失败");
        }
        return result;
    }

    @RequestMapping("update")
    @ResponseBody
    @RequiresPermissions("dataDictionary:update")
    @PermissionName("数据字典更新")
    public JsonResult update(DataDictionary entity){
        JsonResult result = new JsonResult();
        try {
            dataDictionaryService.update(entity);
        }catch (Exception e){
            e.printStackTrace();
            result.markMsg("更新操作失败" + e.getMessage());
        }
        return result;
    }

    @RequestMapping("delete")
    @ResponseBody
    @RequiresPermissions("dataDictionary:delete")
    @PermissionName("数据字典删除")
    public JsonResult delete(Long id){
        JsonResult result = new JsonResult();
        try {
            dataDictionaryService.delete(id);
        }catch (Exception e){
            e.printStackTrace();
            result.markMsg("删除操作失败" + e.getMessage());
        }
        return result;
    }

    @RequestMapping("querydataDictionary")
    @ResponseBody
    //查询所有字典数据
    public List<DataDictionary> querydataDictionary(){
        return dataDictionaryService.list();
    }






}
